package com.zhanghp.class06;

import com.zhanghp.refer.TreeNode;

/**
 * @author zhp
 * @since 2024/1/23 18:36
 */
public class Code04_PathSum_Zuo {


    public static boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        return process(root, sum);
    }

    public static boolean process(TreeNode root, int rest) {
        if (root.left == null && root.right == null) {
            return root.val == rest;
        }
        boolean ans = root.left != null && process(root.left, rest - root.val);
        ans |= root.right != null && process(root.right, rest - root.val);
        return ans;
    }
}
